<?php
namespace Home\Controller;
use think\Controller;

class DataController extends Controller {

    //新增商机表单处理
    public function addProjectHandle(){
        //        echo date("Y-m-d h:i:s");
        //        echo I('addtime');
        $Form   =   D('project');
        $Stage = M('stage');
        if($Form->create()) {
            $data = array(
                'name' => I('name'),
                'amount' => I('amount'),
                'stage' => I('stage'),
                'cdate' => I('cdate'),
                'addtime' => date("Y-m-d h:i:s"),
                'updtime' => date("Y-m-d")
            );
            preg_match_all('/\d+\.\d+|0\.\d+|\d+/',$data['amount'],$arr);
            if(strlen(join('',$arr[0])) <= 19)
                $data['amount'] = join('',$arr[0]);
            else
                $this->error('金额超过数据长度！');
            $sid = $Stage->where(array('sname'=>$data['stage']))->getField('sid');
            if($sid){
                $data['sid'] = $sid;
                $result =   $Form->add($data);
                if($result) {
                    $this->success('操作成功！', U('Index/index'));
                }else{
                    $this->error('写入错误！');
                }
            }
            else{
                $this->error('写入错误！');
            }
        }else{
            $this->error($Form->getError());
        }
    }

    //配置阶段
    public function stageConfHandle(){
        $count = count($_POST);
        $Form   =   D('stage');
        for($i = 1; $i <= $count/2; $i++){
            if(I('sname'.$i) != '' && I('rate'.$i) != ''){
                $data['sname'] = I('sname'.$i);
                $data['rate'] = I('rate'.$i);
                $result = $Form->add($data);
            }
        }
        if($result){
            $this->success('操作成功！');
        }
        else{
            $this->error('失败!');
        }
    }

    //删除商机
    public function delete($id=''){
        $Form = M('Project');
        $result = $Form->delete($id);
        if($result){
            $this->success('操作成功!');
        }
        else{
            $this->error('失败!');
        }
    }

    //修改商机
    public function updatePro(){
        $input = I('data');
        $key = array('pid','name','amount','stage','ostage','cdate');
        foreach($key as $k=>$item){
            if($k == 2){    //正则提取数字
                preg_match_all('/\d+\.\d+|0\.\d+|\d+/',$input[$k],$arr);
                if(strlen(join('',$arr[0])) <= 19)
                    $input[$k] = join('',$arr[0]);
                else
                    echo  json_encode(Array('status' => 'error', 'msg' => '超过长度'));
            }
            $data[$item] = $input[$k];
        }
//        print_r($data);exit;
        $Form   =   D('project');
        $Stage = M('stage');
        if($Form->create($data)) {
            if($data['stage'] != $data['ostage']){
                $data['updtime'] = date("Y-m-d");
            }
            $sid = $Stage->where(array('sname'=>$data['stage']))->getField('sid');
            if($sid){
                $data['sid'] = $sid;
                $result =   $Form->save($data);
                if($result) {
                    echo json_encode(Array('status' => 'done', 'msg' => '操作成功', 'data' => $data));
                }else{
                    echo json_encode(Array('status' => 'error', 'msg' => '您的数据与原始数据相同', 'data' => ''));
                }
            }
            else{
                echo json_encode(Array('status' => 'error', 'msg' => '请输入正确的阶段', 'data' => ''));
            }
        }else{
            echo json_encode(Array('status' => 'error', 'msg' => '数据不能为空！', 'data' => ''));
        }
    }

    //商机标题查重
    public function checkTitle(){
        $pid = I('pid');
        $Form = M('project');
        $data['pid'] = array('neq',$pid);
        $result = $Form->where($data)->order('pid desc')->getField('name',true);
//        print_r($result);
//        exit;
        if($result){
            echo json_encode(Array('status' => 'done', 'msg' => '操作成功', 'data' => $result));
        }
        else{
            echo json_encode(Array('status' => 'error', 'msg' => '网络异常', 'data' => ''));
        }
    }


}

?>